<?php
/*
	Template Name: Page Archive
	Desc: 谨慎使用
*/
?>

<?php get_header(); ?>

<div class="content-area">
	<div class="container">
	
		<?php 
			if(is_post_type_archive()){
				$type = $wp_query->query['post_type']; // if is archive page, get type, use "$wp_query->query['post_type']" instead of "$post -> post_type"
			} else if(is_tax()){// if is_tax()
				$type_taxonomy = $wp_query->query_vars['taxonomy']; // get type taxonomy
				$get_type_taxonomy = get_taxonomy( $type_taxonomy );
				$type_taxonomy_value = $wp_query->query_vars['term'];
				$type = $get_type_taxonomy->object_type[0];
				
				if($type_taxonomy_value){
					$tax_filter = array(
						'taxonomy' => $type_taxonomy,
						'field'    => 'slug',
						'terms' => $type_taxonomy_value,
					);
				} 
				
			} else if(is_page()){
				$type = get_post_meta($post->ID, 'selected-type', true);
			} else {
				$type = 'post';
			}
			$posts_per_page = get_option( 'posts_per_page' );// You can set up the 'posts_per_page' by $type
			get_template_part("template-parts/page-title");
		?>
		
		<?php
			$description = term_description();
         		if ( $description )
				printf( '<div class="archive-meta">%s</div>', $description );
		?>
		
		<div class="page-content">
			<?php
				$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

				$query_args = array(
					'post_type' => $type,
					'posts_per_page' => $posts_per_page,
					'orderby' => 'date',
					'order' => 'DESC',
					'post_status' => 'publish',
					'paged' => $paged,
					'tax_query' => array(
						$tax_filter
					)
				  );
				  // create a new instance of WP_Query
				 $the_query = new WP_Query( $query_args );
				 if( $the_query->have_posts() ) {
					
					echo '<div class="articles-wrapper"><div class="row">'; //start loop
					 while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop
					get_template_part( 'template-parts/type/type', $type);
					endwhile;
					echo '</div></div>';
					
					if ( $the_query->max_num_pages > 1 ) :
						require( 'pagination-query.php');
					endif;
					
				} else {
					echo "No results";
				}// end loop
				
				wp_reset_query();
			?>
		</div>
	
	</div>
</div>

<?php get_footer();?>